<template>
	<view>
		<u-swipe-action v-if="siteList.length > 0"
			v-for="(site, index) in siteList" :key="site.id"
			:options="options" :index="index" :btn-width="btnWidth" 
			@click="click"  :show="site.show" @open="open"
		>
			<view class="item" @tap="chooseAddr(index)">
				<view class="top">
					<view class="name">{{ site.name }}</view>
					<view class="phone">{{ site.phone }}</view>
					<view class="tag">
						<text v-if="site.is_default=='1'" :class="{red:site.is_default=='1'}">默认</text>
					</view>
				</view>
				<view class="bottom">
					{{site.province + site.city + site.county + site.address}}
					<view @tap.stop="toEditAddr(site.id)">
						<u-icon name="edit-pen" :size="40" color="#999999"></u-icon>
					</view>
				</view>
			</view>
		</u-swipe-action>
		
		
		<view v-else class="u-p-t-40">
			<u-empty text="暂无地址信息" mode="list"></u-empty>
		</view>
		<view class="addSite" @tap="toAddAddr">
			<view class="add">
				<u-icon name="plus" color="#ffffff" class="icon" :size="30"></u-icon>新建收货地址
			</view>
		</view>
		<u-modal v-model="show" content="确认删除？" :show-cancel-button="true" :mask-close-able="true" @confirm="delConfirm"></u-modal>
	</view>
</template>

<script>
export default {
	data() {
		return {
			siteList: [],
			btnWidth: 240,
			show: false,
			delIndex: 0,
			options: [
				{
					text: '设为默认',
					style: {
						backgroundColor: '#007aff'
					}
				},
				{
					text: '删除',
					style: {
						backgroundColor: '#dd524d'
					}
				}
			]
		};
	},
	onShow() {
		if(this.$u.utils.isLogin()){
			this.getData();
		}
	},
	methods: {
		async getData() {
			const respData = await this.$u.api.addressList();
			this.siteList = respData.data.map((val, idx) => {
				val.show = false
				return val;
			});
		},
		showModal() {
			this.show = true;
		},
		async delConfirm(){
			let del = this.siteList.splice(this.delIndex, 1);
			await this.$u.api.addressDel(del[0].id);
			this.$u.toast('删除成功');
		},
		async click(index, index1) {
			if(index1 == 1) {
				this.delIndex = index;
				this.showModal()
			} else {
				await this.$u.api.addressDefault(this.siteList[index].id);
				this.$u.toast(`设置成功`);
				// 此处必须延迟100ms才重新请求数据，不然获取的数据还未更新，“默认”字样无法刷新，不知为何。
				setTimeout(()=>{
					this.getData()
				},100)
			}
		},
		// 如果打开一个的时候，不需要关闭其他，则无需实现本方法
		open(index) {
			// 先将正在被操作的swipeAction标记为打开状态，否则由于props的特性限制，
			// 原本为'false'，再次设置为'false'会无效
			this.siteList[index].show = true;
			this.siteList.map((val, idx) => {
				if(index != idx) this.siteList[idx].show = false;
			})
		},
		
		toAddAddr(){
			this.$u.route({
				type: 'navigateTo',
				url: 'pages/address/addAddress'
			});
		},
		toEditAddr(id){
			this.$u.route({
				type: 'navigateTo',
				url: 'pages/address/addAddress?id=' + id
			});
		},
		chooseAddr(index){
			// 判断是否从订单预览页跳转而来
			const orderPrewUrl = uni.getStorageSync('order_prew_url');
			if(orderPrewUrl){
				uni.removeStorageSync('order_prew_url')
				const addr = this.siteList[index];
				console.log(addr);
				this.$u.route({
					type: 'redirectTo',
					url: 'pages/order/orderPrew?addrId=' + addr.id
				});
			}
		}
		
	}
};
</script>

<style lang="scss" scoped>
	
.item {
	padding: 40rpx 20rpx;
	.top {
		display: flex;
		font-weight: bold;
		font-size: 34rpx;
		.phone {
			margin-left: 60rpx;
		}
		.tag {
			display: flex;
			font-weight: normal;
			align-items: center;
			text {
				display: block;
				width: 80rpx;
				height: 34rpx;
				line-height: 34rpx;
				color: #ffffff;
				font-size: 20rpx;
				border-radius: 6rpx;
				text-align: center;
				margin-left: 30rpx;
				background-color:rgb(49, 145, 253);
			}
			.red{
				background-color:red
			}
		}
	}
	.bottom {
		display: flex;
		margin-top: 20rpx;
		font-size: 28rpx;
		justify-content: space-between;
		color: #999999;
	}
}
.addSite {
	display: flex;
	justify-content: space-around;
	width: 600rpx;
	line-height: 100rpx;
	position: absolute;
	bottom: 30rpx;
	left: 80rpx;
	background-color: red;
	border-radius: 60rpx;
	font-size: 30rpx;
	.add{
		display: flex;
		align-items: center;
		color: #ffffff;
		.icon{
			margin-right: 10rpx;
		}
	}
}
</style>
